/*
 * This file is part of Jmesh.
 *
 * last modified: October 02, 2001, 00:30 AM
 *
 */
package br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;


/**
 * A class representing creation dialog on screen.
 * This dialog is provided by some tabs
 * For example:
 * <pre>
 *    InpOutTableDialog dgl = new InpOutTableDialog(title, nLines, nTables);
 *    dlg.addOutputTable(tableName, columnNames);	
 *    if(dlg.setVisible(true) == 0) table = dlg.getOutputTable;
 * </pre>
 *
 * @author Pitangueira, Roque & Brugiolo, Marco 
 * @version 0.0.1 Beta  
 * @since jdk1.4.1
 */

public final class OutputTableDialog extends TabbedDialog {
	
	private static final long serialVersionUID = 1L;
	private String title;
	private int nLines;
	protected JPanel JmainPane = new JPanel();
	OutputTableModel tM;
	private ArrayList<JTable> tables = new ArrayList<JTable>();

	/**
	 * Constructor method.
	 *
	 * @param title   a dialog title.
	 * @see javax.swing
	 */
	public OutputTableDialog(String title, int nLines) {
		if(title == null || nLines <= 0){
		this.showErrorMessage("Erron in InpOutTableDialog constructor"
					+"\nTable Title = "+title
					+"\nTable Numero de Linhas = "+nLines);
		System.exit(0);
		}
		this.title = title;
		this.nLines = nLines;
	}//End of OutputTableDialog

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void addOutputTable(Color c, String name, String[] colNames, Object[][] cells) {
		if(name == null || colNames == null || cells ==null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
		+"\nCells = "+cells);

		System.exit(0);
		}	

		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		
		
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
			button.setMaximumSize(new Dimension(50,10));
			linePane.add(button);
		}
		mainPane.add(linePane);

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(1,1,0,0));
		tM = new OutputTableModel(nLines, colNames.length, colNames, cells);
		JTable table = new JTable(tM);
		table.setName(name);
		table.setBackground(c);	
		ListIterator<JTable> tabs = tables.listIterator();
		tabs.add(table);
			
		tablePane.add(table);
		mainPane.add(tablePane);
		addTab(name, mainPane);
		this.setMainPane(mainPane);
	} //End of addOutputTable

	
	public void addOutputTable(Color c, String name, String[] colNames, JButton[][] cells) {
		if(name == null || colNames == null || cells ==null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
		+"\nCells = "+cells);

		System.exit(0);
		}	

		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		
		
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
			button.setMaximumSize(new Dimension(50,10));
			linePane.add(button);
		}
		mainPane.add(linePane);

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(nLines,colNames.length,0,0));
		for(int i=0; i < nLines; i++)
		for(int j=0; j < colNames.length; j++)
		{
			tablePane.add(cells[i][j]);
		}

		mainPane.add(tablePane);
		addTab(name, mainPane);
		this.setMainPane(mainPane);
	} //End of addOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void addOutputTable(Color c, String name, String[] colNames, double[][]cells) {
		if(name == null || colNames == null || cells == null){
		this.showErrorMessage("Erron in addInputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
					+"\nCells = "+cells);
		}	
		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		Double objCells[][] = new Double[nLines][colNames.length];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<colNames.length; j++){
			objCells[i][j] = new Double(cells[i][j]);
		}
		this.addOutputTable(c, name, colNames, objCells);
	} //End of addOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void addOutputTable(Color c, String name, String[] colNames, int[][]cells) {
		if(name == null || colNames == null || cells == null){
		this.showErrorMessage("Erron in addInputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
					+"\nCells = "+cells);
		}	
		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		Integer objCells[][] = new Integer[nLines][colNames.length];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<colNames.length; j++){
			objCells[i][j] = new Integer(cells[i][j]);
		}
		this.addOutputTable(c, name, colNames, objCells);
	} //End of addOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void addOutputTable(Color c, String name, String[] colNames, boolean[][]cells) {
		if(name == null || colNames == null || cells == null){
		this.showErrorMessage("Erron in addInputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
					+"\nCells = "+cells);
		}	
		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		Boolean objCells[][] = new Boolean[nLines][colNames.length];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<colNames.length; j++){
			objCells[i][j] = new Boolean(cells[i][j]);
		}
		this.addOutputTable(c, name, colNames, objCells);
	} //End of addOutputTable

	
	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void addOutputTable(String name, String[] colNames) {
		if(name == null || colNames == null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames);
		return;
		}	

		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
			linePane.add(button);
		}
		mainPane.add(linePane);

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(1,1,0,0));
		tM = new OutputTableModel(nLines, colNames.length, colNames);
		JTable table = new JTable(tM);
		tablePane.add(table);
		mainPane.add(tablePane);
		addTab(name, mainPane);
		
	} //End of addOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTable(Object[][] obj) {
		tM.initOutputTable(obj);
	} //End of initOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTable(double init) {
		int nC = tM.getColumnCount();
		Double objCells[][] = new Double[nLines][nC];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<nC; j++)
			objCells[i][j] = new Double(init);
		tM.initOutputTable(objCells);
	} //End of initOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTable(int init) {
		int nC = tM.getColumnCount();
		Integer objCells[][] = new Integer[nLines][nC];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<nC; j++)
			objCells[i][j] = new Integer(init);
		tM.initOutputTable(objCells);
	} //End of initOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTable(boolean init) {
		int nC = tM.getColumnCount();
		Boolean objCells[][] = new Boolean[nLines][nC];
		for(int i=0; i<nLines; i++)
			for(int j=0; j<nC; j++)
			objCells[i][j] = new Boolean(init);
		tM.initOutputTable(objCells);
	} //End of initOutputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTableColumn(int column, Object[] obj) {
		tM.initOutputTableColumn(column, obj);
	} //End of initInputTable

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTableColumn(int column, double init) {
		if(column < 0){
		this.showErrorMessage("Erron in addInputTable method"
					+"\nColumn Number = "+column);
		return;
		}	
		Double objCells[] = new Double[nLines];
		for(int i=0; i<nLines; i++)
			objCells[i] = new Double(init);
		tM.initOutputTableColumn(column, objCells);
	} //End of initInputTableColumn

	/**
	 * Method to add an input table.
	 *
	 * @param name  a table name.
	 * @param columsTitles an array with the table column names.
	 * @see javax.swing
	 */
	public void initOutputTableColumn(int column, double[]cells) {
		if(column < 0 || cells == null){
		this.showErrorMessage("Erron in addInputTable method"
					+"\nColumn Number = "+column
					+"\nCells = "+cells);
		return;
		}	
		if(cells.length != nLines){
		this.showErrorMessage("Erron in initInputColumnTable method"
					+"\nThe Number of Lines have to be "+nLines);
		return;
		}

		Double objCells[] = new Double[nLines];
		for(int i=0; i<nLines; i++)
			objCells[i] = new Double(cells[i]);
		tM.initOutputTableColumn(column, objCells);
	} //End of initOutputTableColumn

	/**
	 * Method to show the dialog.
	 *
	 * @return  an integer indicating the user option: 1 = Cancel, 0 = OK.
	 * @see javax.swing
	 */
	public int showDialog() {
		return super.showDialog(new JFrame(), title);
	}

	/**
	 * Method to be executed for the OK option.
	 *
	 */
	public void approvedPerform() {
		try {
			this.setVisible(false);
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}

	private void showErrorMessage(String msg){
		JOptionPane.showMessageDialog(null,msg,"Error!",JOptionPane.INFORMATION_MESSAGE);
	}//End of showErrorMessage
	
	public JPanel getMainPane(){
		return JmainPane;
		
	}
	
	public void setMainPane(JPanel mp){
		JmainPane = mp;
		
	}
	
	public JTabbedPane getTablePane(){
	    return super.getTablePane();
	}

	public JTable getTable(String name){
	    int table = IndexOf(name);
	    return (JTable)this.tables.get(table);
	}
	
	public JTable getTable(int i){
	    return (JTable)this.tables.get(i);
	}
	
	public Color getTableColor(String name){
	    int table = IndexOf(name);
	    return ((JTable)this.tables.get(table)).getBackground();
	}
	
	public void setTableColor(String name, Color c){
	    int table = IndexOf(name);
	    ((JTable)this.tables.get(table)).setBackground(c);
	}
	
	public Object getCell(String name, int row, int col){
	    int table = IndexOf(name);
	    return ((JTable)this.tables.get(table)).getModel().getValueAt(row, col);
	}
	
	public String getColumnName(String name, int col){
	    int table = IndexOf(name);
	    return ((JTable)this.tables.get(table)).getModel().getColumnName(col);
	}
	
	private int IndexOf(String name){
	    ListIterator tabs = tables.listIterator();
	    while(tabs.hasNext()){
	       JTable t = (JTable)tabs.next();
	       String n = t.getName();
	       if(n.equals(name)) return tables.indexOf(t);
	    }
	    return 0;
	}
	
	
}// End of class

//==== End of File
